<?php

class Magentochina_Bundlepromotion_Block_Adminhtml_Bundlepromotion_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
  protected function _prepareForm()
  {
      $form = new Varien_Data_Form();
      $this->setForm($form);
      $fieldset = $form->addFieldset('bundlepromotion_form', array('legend'=>Mage::helper('bundlepromotion')->__('Item information')));
     
      $fieldset->addField('title', 'text', array(
          'label'     => Mage::helper('bundlepromotion')->__('Title'),
          'class'     => 'required-entry',
          'required'  => true,
          'name'      => 'title',
      ));
	  
	  $fieldset->addField('simple_action', 'select', array(
            'name' => 'simple_action',
            'label'     => Mage::helper('salesrule')->__('Apply'),
            'title'     => Mage::helper('salesrule')->__('Apply'),       
			'options'    => array(
                'by_percent' => Mage::helper('salesrule')->__('Percent of product price discount'),
                'by_fixed' => Mage::helper('salesrule')->__('Fixed amount discount'),                
            ),
        ));
	  
	  $fieldset->addField('discount_amount', 'text', array(
            'name' => 'discount_amount',
            'label'     => Mage::helper('salesrule')->__('Discount amount '),
            'title'     => Mage::helper('salesrule')->__('Discount amount '),            
        ));
		
	  
	  $fieldset->addField('product_ids', 'text', array(
            'name' => 'product_ids',
            'label'     => Mage::helper('salesrule')->__('Bundled Products'),
            'title'     => Mage::helper('salesrule')->__('Bundled Products'),            
        ));
		$promotion = Mage::getModel("bundlepromotion/bundlepromotion")->load( $this->getRequest()->getParam('id') );
		
		$form->addReplacement( "<input[^>]*name=\"product_ids\"[^>]*>",
							'<fieldset id="rule_conditions_fieldset" >
    <input id="conditions__1__type" name="rule[conditions][1][type]" value="salesrule/rule_condition_combine" class="hidden" type="hidden"/>
	<ul id="conditions__1__children" class="rule-param-children"><li>
		<input id="conditions__1--3__type" name="rule[conditions][1--3][type]" value="salesrule/rule_condition_product_found" class="hidden" type="hidden"/>
		<input type="hidden" id="conditions__1--3__value" name="rule[conditions][1--3][value]"/>
		<input type="hidden" id="conditions__1--3__aggregator" name="rule[conditions][1--3][aggregator]"/>
		<input id="conditions__1--3--1__type" name="rule[conditions][1--3--1][type]" value="salesrule/rule_condition_product" class="hidden" type="hidden"/>
		<input type="hidden" class="hidden" id="conditions__1--3--1__attribute" name="rule[conditions][1--3--1][attribute]" value="sku"/> Product ID&nbsp; 
		<span class="rule-param">
			<a href="javascript:void(0)" class="label">'.($promotion->getData('product_ids')?$promotion->getData('product_ids'):'Select Products').'</a>
			<span class="element"> 
				<input id="conditions__1--3--1__value" name="product_ids" value="'.$promotion->getData('product_ids').'" type="text" class=" input-text"/>
				<a href="javascript:void(0)" class="rule-chooser-trigger">
					<img src="'.Mage::getBaseUrl('web').'skin/adminhtml/default/default/images/rule_chooser_trigger.gif" alt="" class="v-middle rule-chooser-trigger" title="Open Chooser" />
				</a> 
				<a href="javascript:void(0)" class="rule-param-apply">
					<img src="'.Mage::getBaseUrl('web').'skin/adminhtml/default/default/images/rule_component_apply.gif" class="v-middle" alt="Apply" title="Apply" />
				</a> 
			</span>
		</span>&nbsp; 
		<br />Skus : '.$promotion->getData('models').'
		<div class="rule-chooser" url="'.Mage::getBaseUrl('web').'index.php/'.$this->getBackendName().'/promo_widget/chooser/attribute/sku/form/rule_conditions_fieldset/choose_attribute/entity_id/key/'.Mage::getModel('adminhtml/url')->getSecretKey('promo_widget', 'chooser').'/"></div>
	</li></ul> 
</fieldset>
</div>
<script type="text/javascript">
var rule_conditions_fieldset = new VarienRulesForm(\'rule_conditions_fieldset\', \''.Mage::getBaseUrl('web').'index.php/'.$this->getBackendName().'/promo_quote/newConditionHtml/form/rule_conditions_fieldset/key/'.Mage::getModel('adminhtml/url')->getSecretKey('promo', 'widgetchooser').'/\');
</script>' );
		
		$form->addReplacement( "VarienRulesForm\('rule_actions_fieldset',",
							        "VarienRulesForm('promo_catalog_edit_tabs_actions_section_content'," );
		
		$customerGroups = Mage::getResourceModel('customer/group_collection')
            ->load()->toOptionArray();

        $found = false;
        foreach ($customerGroups as $group) {
            if ($group['value']==0) {
                $found = true;
            }
        }
        if (!$found) {
            array_unshift($customerGroups, array('value'=>0, 'label'=>Mage::helper('salesrule')->__('NOT LOGGED IN')));
        }
		
		$fieldset->addField('customer_group_ids', 'multiselect', array(
            'name'      => 'customer_group_ids[]',
            'label'     => Mage::helper('salesrule')->__('Customer Groups'),
            'title'     => Mage::helper('salesrule')->__('Customer Groups'),
            'required'  => true,
            'values'    => $customerGroups,
        ));
		
				
      $fieldset->addField('status', 'select', array(
          'label'     => Mage::helper('bundlepromotion')->__('Status'),
          'name'      => 'status',
          'values'    => array(
              array(
                  'value'     => 1,
                  'label'     => Mage::helper('bundlepromotion')->__('Enabled'),
              ),

              array(
                  'value'     => 2,
                  'label'     => Mage::helper('bundlepromotion')->__('Disabled'),
              ),
          ),
      ));
     
      if ( Mage::getSingleton('adminhtml/session')->getBundlepromotionData() )
      {
          $form->setValues(Mage::getSingleton('adminhtml/session')->getBundlepromotionData());
          Mage::getSingleton('adminhtml/session')->setBundlepromotionData(null);
      } elseif ( Mage::registry('bundlepromotion_data') ) {
          $form->setValues(Mage::registry('bundlepromotion_data')->getData());
      }
      return parent::_prepareForm();
  }
  
	private function getBackendName()
	{
		$dirs = explode( DS.'app'.DS.'code',__FILE__);
		$local_xml = $dirs[0].DS.'app'.DS.'etc'.DS.'local.xml';		
		$xml = simplexml_load_string( file_get_contents($local_xml),'SimpleXMLElement',LIBXML_NOCDATA | LIBXML_NOBLANKS );
		return $xml->admin->routers->adminhtml->args->frontName;
	}
}